<template>
  <!-- 版心 -->
  <el-container class="main-container">
    <!-- 1.左侧菜单栏 -->
    <el-aside width="200px">
      <div class="user-box">
        <img v-if="user.user_pic" :src="user.user_pic" alt="" />
        <img v-else src="@/assets/logo.png" alt="" />
        <span>欢迎 {{ user.nickname || user.username }}</span>
      </div>
      <!-- 左侧菜单 -->
      <el-menu
        :default-active="$route.path"
        class="el-menu-vertical-demo"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
        unique-opened
        router
      >
        <template v-for="item in menus">
          <!-- 一级菜单 -->
          <el-menu-item
            v-if="!item.children"
            :index="item.indexPath"
            :key="item.indexPath"
          >
            <i :class="item.icon"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
          <!-- 文章管理 -->
          <el-submenu v-else :index="item.indexPath" :key="item.indexPath">
            <template #title>
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </template>
            <!-- 循环渲染“二级菜单” -->
            <el-menu-item
              v-for="ele in item.children"
              :index="ele.indexPath"
              :key="ele.indexPath"
            >
              <i :class="ele.icon"></i>
              <span slot="title">{{ ele.title }}</span>
            </el-menu-item>
          </el-submenu>
        </template>
      </el-menu>
    </el-aside>
    <!-- 2.右侧内容 -->
    <el-container>
      <!-- 2.1右侧顶部栏 -->
      <el-header>
        <!-- 左侧logo -->
        <div class="logo"></div>
        <!-- 右侧menu -->
        <el-menu mode="horizontal" router>
          <el-menu-item>
            <img
              v-if="user.user_pic"
              :src="user.user_pic"
              alt=""
              width="32px"
            />
            <img v-else src="@/assets/logo.png" alt="" width="32px" />
          </el-menu-item>
          <!-- 个人中心 -->
          <el-submenu index="3">
            <template #title>
              <span>个人中心</span>
            </template>
            <!-- 循环渲染“二级菜单” -->
            <el-menu-item index="/user-info">
              <i class="el-icon-s-operation"></i>
              <span slot="title">基本资料</span>
            </el-menu-item>
            <el-menu-item index="/user-avatar">
              <i class="el-icon-camera"></i>
              <span slot="title">更换头像</span>
            </el-menu-item>
            <el-menu-item index="/user-pwd">
              <i class="el-icon-key"></i>
              <span slot="title">重置密码</span>
            </el-menu-item>
          </el-submenu>
          <el-menu-item>
            <i class="el-icon-switch-button"></i>
            <span slot="title" @click="doLogout">退出</span>
          </el-menu-item>
        </el-menu>
      </el-header>
      <!-- 2.2右侧主体内容 -->
      <el-main>
        <!-- 二级路由出口 -->
        <router-view></router-view>
      </el-main>
      <!-- 2.3右侧底部栏 -->
      <el-footer>© www.itheima.com - 黑马程序员</el-footer>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: []
    }
  },
  // methods:事件处理函数
  methods: {
    async doLogout () {
      // 弹出确认框
      const res = await this.$confirm('您确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {})
      // 确认退出 清除token 跳转登录页
      if (res) {
        this.$store.commit('setToken', '')
        this.$router.push('/login')
      }
    }
  },
  // 计算属性
  computed: { 
    user () {
      return this.$store.state.user
    }
  },
  async created () {
    // 个人信息请求提交到actions更新
    this.$store.dispatch('getUserInfo')
    // 获取左侧菜单栏
    const res = await this.$axios.get('/my/menus')
    // 添加数据
    this.menus = res.data.data
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

//右侧样式
.el-header {
  .logo {
    height: 45px;
    width: 150px;
    padding: 8px 0px;
    user-select: none;
    text-align: center;
    background: url('@/assets/images/logo.png') center center / 100px no-repeat
      rgb(85, 85, 85);
    border-radius: 3px;
    margin-left: 15px;
  }
}

// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
